package com.jamal2362.apktool.util;

import android.content.Context;
import android.graphics.Color;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import org.json.JSONObject;
import com.jamal2362.apktool.R;
import com.jamal2362.apktool.MainActivity;
import com.myopicmobile.textwarrior.common.ColorScheme;

public class EditorTheme
{
	public final String[] themeParams = {"FOREGROUND", "BACKGROUND", "SELECTION_FOREGROUND", "SELECTION_BACKGROUND", "CARET_BACKGROUND", "CARET_DISABLED", "LINE_HIGHLIGHT", "NON_PRINTING_GLYPH", "COMMENT", "KEYWORD", "NAME", "LITERAL", "OPERATOR", "SEPARATOR", "PACKAGE", "TYPE", "LINENUMBER_BG", "LINENUMBER_FG", "ERROR"};
	public static String themePath;

	public EditorTheme()
	{
		themePath = Settings.editorThemePath;
	}

	public static int getv(String name, int mode)
	{
		try
		{
			String theme = getJsonTheme(new File(themePath));
			String tht = (mode == 0 ? "LIGHT" : "DARK");
			JSONObject jo = new JSONObject(theme);
			JSONObject type = (JSONObject) jo.get(name);
			return Color.parseColor((String)type.get(tht));
		}
		catch (Exception e)
		{
			return -1;
		}
	}

	public static String getJsonTheme(File f)
	{
		String jout = "";
		String  line;
		try
		{
			FileInputStream in = new FileInputStream(f);
			BufferedReader reader = new BufferedReader(new InputStreamReader(in));
			while ((line = reader.readLine()) != null)
			{
				jout += line + "\n";
			}
		}
		catch (Exception e)
		{
			return null;
		}
		return jout;
	}

	public boolean isValid(File f)
	{
		try
		{
			String theme = getJsonTheme(f);
			JSONObject jo = new JSONObject(theme);
			for (int i = 0; i < themeParams.length; i++)
			{
				if (!jo.has(themeParams[i]))
					return false;
				JSONObject type = (JSONObject) jo.get(themeParams[i]);
				if (!type.has("LIGHT") || !type.has("DARK"))
					return false;
			}
			return true;
		}
		catch (Exception e)
		{
			return false;
		}
	}
	
	public void importTheme(Context c, File f)
	{
		boolean res = false;
		try
		{
			if (isValid(f))
			{
				String in = f.getName();
				int i = in.lastIndexOf('.');
				String[] sn = c.getResources().getStringArray(R.array.editor_themes_values);
				for (String tn : sn)
				{
					if (in.substring(0, i).equals(tn))
						in = "imp_" + in;
				}
				File tp = new File(themePath);
				String name = FileUtils.genName(c, tp.getParent(), in.substring(0, i), in.substring(i + 1), 0);
				File tf = new File(tp.getParent() + "/" + name);
				if (FileUtils.copyFiles(f, tf))
				{
					int j = name.lastIndexOf(".");
					Settings.editorTheme = name.substring(0, j);
					Settings.editorThemePath = tf.getAbsolutePath();
					Settings.sets(c, "editor_theme", Settings.editorTheme);
					res = true;
				}
				else
				{
					res = false;
				}
			}
			else
			{
				res = false;
			}
		}
		catch(Exception e)
		{
			res = false;
		}
		if (res)
		{
			TextUtil.st(c, c.getString(R.string.complete));
			ColorScheme.Colorable.init();
			MainActivity.INSTANCE.recreate();
		}
		else
		{
			TextUtil.st(c, c.getString(R.string.error));
		}
	}
}
